﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FreedomX2.Core.UI
{
    /// <summary>
    /// WPFKeybord.xaml 的交互逻辑
    /// </summary>
    public partial class WPFKeybord : UserControl
    {
        public WPFKeybord()
        {
            InitializeComponent();

            this.Margin = new Thickness(-5, 425, 0, 0);
        }

        public delegate void Enter_Click(object sender, EventArgs e);
        public event Enter_Click Press_Enter = null;
        public void ChangeSFT()
        {
            foreach (UIElement ue in Keyboard.Children)
            {
                var vbutton = ue as Button;
                if (vbutton != null)
                {
                    if (vbutton.Content.ToString() != "Sft")
                    {
                        if (!SFT)
                        {
                            vbutton.Content = vbutton.Content.ToString().ToLower();
                        }
                        else
                        {
                            vbutton.Content = vbutton.Content.ToString().ToUpper();
                        }
                    }
                }
            }
            if (!SFT)
            {
                SFT = true;
                Sft.Background = Brushes.LightGray;
                Sft.Foreground = Brushes.Black;
            }
            else
            {
                SFT = false;
                Sft.Background = Brushes.Black;
                Sft.Foreground = Brushes.LightGray;
            }
        }
        public bool SFT = false;
        public TextBox InputTarget { get; set; }
        private void Keyboard_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var vButton = e.Source as Button;
            if (InputTarget != null && vButton != null)
            {
                var vInputString = vButton.Content.ToString().Trim();
                if (vInputString == "确认")
                {
                    if (Press_Enter != null) Press_Enter.Invoke(vButton, e);
                }
                else if (vInputString == "删除")
                {
                    if (InputTarget.Text.Length > 0)
                        InputTarget.Text = InputTarget.Text.Substring(0, InputTarget.Text.Length - 1);

                }
                else if (vInputString == "Sft")
                {
                    ChangeSFT();
                }
                else
                {
                    InputTarget.Text += vInputString;
                }
            }
        }
    }
}
